package com.sh.data.engine.application.service.dataquality;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.sh.data.engine.application.model.dto.base.PagerResponse;
import com.sh.data.engine.application.model.dto.dataquality.DataQualityJobResponse;
import com.sh.data.engine.application.model.mapper.dataquality.DataQualityJobResponsePageMapper;
import com.sh.data.engine.application.model.request.dataquality.DataQualityTaskRequest;
import com.sh.data.engine.common.util.ServletUtils;
import com.sh.data.engine.domain.base.model.PageResult;
import com.sh.data.engine.domain.dataquality.model.domain.DataQualityJobDomain;
import com.sh.data.engine.domain.dataquality.model.domain.DataQualityJobRuleDetailDomain;
import com.sh.data.engine.domain.dataquality.model.domain.DataqualityJobMonitorDomain;
import com.sh.data.engine.domain.dataquality.model.domain.DataqualityJobMonitorDomain2;
import com.sh.data.engine.domain.dataquality.model.param.DataQualityTaskParam;
import com.sh.data.engine.domain.dataquality.service.DataQualityJobService;
import com.sh.data.engine.domain.util.ConvertUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * @author: mengzheng.mhc
 * @date: 2024/7/30 9:49
 */
@Slf4j
@Service
public class DataQualityJobApplicationService {

    @Autowired
    private DataQualityJobService dataQualityJobService;

    @Autowired
    private DataQualityJobResponsePageMapper dataQualityJobResponsePageMapper;

    public PagerResponse<DataQualityJobResponse> getJobs(DataQualityTaskRequest request) {
        DataQualityTaskParam dataQualityTaskParam =
            ConvertUtil.copyProperties(request, DataQualityTaskParam.class);
        Long projectId = ServletUtils.getProjectId();
        IPage<DataQualityJobDomain> jobs =
            dataQualityJobService.getJobs(dataQualityTaskParam, projectId);
        PageResult<DataQualityJobDomain> pageResult = PageResult.convert(jobs);
        return dataQualityJobResponsePageMapper.toPageResponse(pageResult);
    }

    public PageResult<DataQualityJobRuleDetailDomain> getJobRuleDetail(Long jobId, Integer pageNum, Integer pageSize) {
        if (jobId == null) {
            return dataQualityJobService.getJobRuleDetails(null, pageNum, pageSize);
        } else {
            return dataQualityJobService.getJobRuleDetails(jobId, pageNum, pageSize);
        }
    }

    public PageResult<DataqualityJobMonitorDomain2> getJobRuleFields(Long jobId, Integer pageNum, Integer pageSize) {
        Long jobParamId = null;
        if (jobId != null) {
            jobParamId = jobId;
        }
        DataqualityJobMonitorDomain jobRuleFields =
            dataQualityJobService.getJobRuleFields(jobParamId, pageNum, pageSize);

        // 应前端要求，对数据重新封装
        PageResult<DataqualityJobMonitorDomain.DataqualityJobRuleFieldExt> pageInfo =
            jobRuleFields.getPageInfo();
        PageResult<DataqualityJobMonitorDomain2> dataqualityJobMonitorDomain2PageResult =
            ConvertUtil.convertPageInfo(pageInfo, DataqualityJobMonitorDomain2.class);
        for (DataqualityJobMonitorDomain2 dataqualityJobMonitorDomain2 :
            dataqualityJobMonitorDomain2PageResult.getResult()) {
            // 塞入tableName，每个tableName都是一样的
            dataqualityJobMonitorDomain2.setTableName(jobRuleFields.getTableName());
        }
        return dataqualityJobMonitorDomain2PageResult;
    }
}
